package com.wsl.conyroller;

import com.wsl.pojo.ResultPojo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @Author:WangShuliang
 * @Description:
 * @Date:Created in 18:30 2018/1/8
 * @Modified by:
 */
@Controller
public class OracleController {
    Log log = LogFactory.getLog(this.getClass());
    @Autowired
    @Qualifier("jdbcTemplate")
    private JdbcTemplate primaryJdbcTemplate;

    @Autowired
    @Qualifier("basejdbcTemplate")
    private JdbcTemplate baseJdbcTemplate;

    @RequestMapping("getObjectOracle")
    @ResponseBody
    public String getObjectOracle() {
        List<ResultPojo> resultPojos = new ArrayList<ResultPojo>();
        log.info("开始进行比较。。。");
        String sql = "select   * from  User_Tab_Columns where table_name='STUDENT'";
        String tableName="STUDENT";
        List listSpringMaster = primaryJdbcTemplate.queryForList(sql);
        List listWsl = baseJdbcTemplate.queryForList(sql);
        for (int i = 0; i < listSpringMaster.size(); i++) {
            Map mapSpring = (Map) listSpringMaster.get(i);
            for (int j = 0; j < listWsl.size(); j++) {
                Map mapWsl = (Map) listWsl.get(j);
                String columnName = mapSpring.get("COLUMN_NAME").toString();
                String wslColumnName = mapWsl.get("COLUMN_NAME").toString();
                if (columnName.equals(wslColumnName)) {//校验是否为同一个字段
                    String dataTypeSpring = mapSpring.get("DATA_TYPE").toString();
                    String dataTypeWsl = mapWsl.get("DATA_TYPE").toString();
                    if (dataTypeSpring.equals(dataTypeWsl)) {//校验字段的类型是否一致
                        if (dataTypeSpring.equals("VARCHAR2") || dataTypeSpring.equals("CHAR")) {
                            BigDecimal lengthSpring = (BigDecimal) mapSpring.get("DATA_LENGTH");
                            BigDecimal lengthWsl = (BigDecimal) mapWsl.get("DATA_LENGTH");
                            if (lengthSpring.equals(lengthWsl)){//校验字段的长度是否一致

                            } else {
                                ResultPojo  resultPojo = new ResultPojo();
                                resultPojo.setTableName(tableName);
                                resultPojo.setColumnName(columnName);
                                resultPojo.setFirstDataLength(lengthSpring);
                                resultPojo.setSecondDataLength(lengthWsl);
                                resultPojos.add(resultPojo);
                                log.info("字段: "+columnName+": spring_Master的" + lengthSpring + " 和WSL的=" + lengthWsl + "长度不一致");
                            }
                        }
                    } else {
                        ResultPojo  resultPojo = new ResultPojo();
                        resultPojo.setTableName(tableName);
                        resultPojo.setColumnName(columnName);
                        resultPojo.setFirstDataType(dataTypeSpring);
                        resultPojo.setSecondDataType(dataTypeWsl);
                        resultPojos.add(resultPojo);
                        log.info("字段: spring_Master的columnName=" + columnName + " 和WSL的columnName=" + wslColumnName + "类型不一致");
                    }
                }
            }
        }
        return resultPojos.toString();
    }
}